<template>
  <view class="content">
    <view style="background: #fff">
      <basics-form ref="basicsForm" hiddenMustIcon @submit="submit" />
    </view>
    <view style="height: 200rpx" />
    <foot-button style="position: fixed; bottom: 0rpx; z-index: 10" :buttonList="buttonList" @tapButton="tapButton" />
  </view>
</template>

<script>
import basicsForm from "@/components/basics-form/basics-form.vue";
import footButton from "@/components/foot-button/foot-button.vue";

export default {
  components: {
    basicsForm,
    footButton,
  },
  data() {
    return {
      patientId: uni.getStorageSync("patientId"),
      familyDetail: null,
      relationList: null,
      buttonList: [
        {
          name: `重置`,
          icon: "zhongzhi-",
        },
        {
          name: `保存`,
          icon: "save",
        },
      ],
    };
  },
  onLoad(options) {
    if (options.isDetail) {
      this.familyDetail = uni.getStorageSync("familyDetail");
      uni.setNavigationBarTitle({ title: "亲友详情" });
    }
    this.patientRelationList();
  },
  methods: {
    //亲友关系
    patientRelationList() {
      this.relationList = uni.getStorageSync("relationList");
      if (this.relationList) {
        this.initRules();
      } else {
        this.$api.patientRelationList().then((res) => {
          if (res.code == 200) {
            this.relationList = res.data;
            this.initRules();
            uni.setStorage({
              key: "relationList",
              data: res.data,
            });
          }
        });
      }
    },
    initRules() {
      let relation = "";
      if (this.familyDetail) {
        let index = this.relationList.indexOf(this.familyDetail.relation);
        if (index > -1) {
          relation = index;
        }
      }
      let rules = [
        {
          title: "姓名",
          key: "name",
          value: this.familyDetail ? this.familyDetail.name : "",
        },
        {
          title: "手机号",
          key: "phone",
          value: this.familyDetail ? this.familyDetail.phone : "",
          type: "number",
          maxlength: 11,
          ruleList: ["isMobile"],
          ruleMsgList: ["请输入正确的手机号码"],
        },
        {
          title: "关系",
          key: "relation",
          value: relation,
          type: "select",
          selectList: this.relationList,
        },
      ];

      this.$nextTick(() => {
        this.$refs.basicsForm.initForm(rules);
      });
    },
    tapButton(index) {
      if (index) {
        this.$refs.basicsForm.formValid();
      } else {
        this.$refs.basicsForm.formReset();
      }
    },
    submit(param) {
      if (!param) {
        return;
      }
      uni.showModal({
        title: "提示",
        content: "确认提交？",
        success: (res1) => {
          if (res1.confirm) {
            param.patientId = this.patientId;
            param.relation = this.relationList[param.relation];
            if (this.familyDetail) {
              param.familyId = this.familyDetail.familyId;
            }
            this.$api[param.familyId ? "patientSaveFamily" : "patientAddFamily"](param).then((res) => {
              if (res.code == 200) {
                this.$util.showSuccessToast("保存成功");
                setTimeout(() => {
                  this.$util.backToRefreshPrevPage("family/index");
                }, 1500);
              }
            });
          }
        },
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.content{
  background: $back-grey-color;
  height: 100vh;
}
</style>
